package com.fresh.system.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.fresh.entity.Orders;
import com.fresh.entity.User;
import com.fresh.system.service.OrdersService;
import com.fresh.system.service.UserService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Wx
 * @Date:2023/08/21/15:13
 * @Description:
 */
@Slf4j
@Component
public class Myjob {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;

   @XxlJob("MyJob")
    public void myjob(){
        List<Orders> list = ordersService.list();
        for (Orders orders : list){
            if (orders.getStatus() == "n") {
                User user = userService.getById(orders.getClientId());
                user.getEmail();
                try {
                    log.info("=======进入QQ邮件发送方法=======");
                    MailAccount account = new MailAccount();
                    account.setHost("smtp.qq.com");
                    account.setPort(587);
                    account.setAuth(true);
                    account.setFrom("2391677486@qq.com");
                    account.setUser("2391677486@qq.com");
                    account.setPass("nesxlsgbncttdhhd");
//                    MailUtil.send(account, CollUtil.newArrayList(user.getEmail()),
                    MailUtil.send(account, CollUtil.newArrayList("2391677486@qq.com"),
                            "购物消息", "你有一笔订单超时未处理", false);
                    log.info("=======QQ邮件发送成功=======");
                } catch (Exception e) {
                    log.error("=======QQ邮件发送失败======="+e.getMessage(),e);
                }
            }
        }
        System.out.println(111);
    }
}
